import 'package:flutter/material.dart';

class FormPage extends StatefulWidget {
  const FormPage({super.key, required this.title});

  final String title;

  @override
  State<FormPage> createState() => _FormPageState();
}

class _FormPageState extends State<FormPage> {

  // 通过controller 监听值 还可以设置默认值
  TextEditingController _selectionController = TextEditingController();


  // 定义变量 通过change事件接受值
  String _text = "";

  @override
  Widget build(BuildContext context) {
    // 可以设置默认值 并进行一些操作
    _selectionController.text = "hello world";
    _selectionController.selection=TextSelection(
        baseOffset: 2,
        extentOffset: _selectionController.text.length
    );


    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text(widget.title),
        ),
        body: Center(
            child: Column(
              children: [
                TextField(
                  controller:_selectionController,
                  onChanged: (v) {
                    setState(() {
                      _text = v;
                    });
                    print(_selectionController.text);
                  },
                  autofocus: true,
                  decoration: InputDecoration(
                      labelText: "用户名",
                      hintText: "用户名或邮箱",
                      prefixIcon: Icon(Icons.person)
                  ),
                ),
                TextField(
                  controller:  _selectionController,
                  decoration: InputDecoration(
                      labelText: "密码",
                      hintText: "您的登录密码",
                      prefixIcon: Icon(Icons.lock)
                  ),
                  obscureText: true,
                ),
              ],
            )));
  }
}
